<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/JavaScript" src="javascript.js"></script>
        <SCRIPT language="JavaScript">

            function adicionaDisciplina() {
                var codDisciplina = document.getElementById("prerequisito").value;
                var nomeDisciplina = getSelectedText("prerequisito");

                var disciplinas = document.getElementById("listaPreRequisitos").value;
                var lista = disciplinas.split(",");
                var jaExiste = false;
                for (x = 0; x < lista.length; x++) {
                    if (lista[x] == codDisciplina) {
                        jaExiste = true;
                    }

                }
                if (jaExiste == false) {
                    document.getElementById("listaPreRequisitos").value = document.getElementById("listaPreRequisitos").value + codDisciplina + ",";
                    var table = document.getElementById("tbPreRequisitos");
                    var rowCount = table.rows.length;
                    var row = table.insertRow(rowCount);
                    var cell0 = row.insertCell(0);
                    cell0.innerHTML = codDisciplina;
                    var cell1 = row.insertCell(1);
                    cell1.innerHTML = nomeDisciplina;
                    var cell2 = row.insertCell(2);

                    cell2.innerHTML = "<a href='#' onclick='removeLinha(" + codDisciplina + ")'>Remover</a>";
                } else {
                    alert("A disciplina já existe na lista de Pre Requisitos.")
                }


            }

            function getSelectedText(elementId) {
                var elt = document.getElementById(elementId);
                if (elt.selectedIndex == -1)
                    return null;

                return elt.options[elt.selectedIndex].text;
            }



            function removeLinha(codDisciplina) {
                var table = document.getElementById("tbPreRequisitos");

                var rowCount = table.rows.length;

                for (var i = 1; i < rowCount; i++) {
                    var row = table.rows[i];
                    var cod = row.cells[0].innerHTML;
                    if (null != cod && cod == codDisciplina) {
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
                }
                var disciplinas = document.getElementById("listaPreRequisitos").value;
                document.getElementById("listaPreRequisitos").value = "";
                var lista = disciplinas.split(",");
                for (x = 0; x < lista.length; x++) {
                    if (lista[x] != codDisciplina && lista[x] != "") {
                        document.getElementById("listaPreRequisitos").value = document.getElementById("listaPreRequisitos").value + lista[x] + ",";
                    }

                }
            }


        </SCRIPT>
        <title>SCA - Manter Disciplina</title>
    </head>
    <body>
        <h1>Manter Disciplina - ${operacao}</h1>

        <s:form action="Disciplina">
            <s:hidden name="operacao" value="%{operacao}"></s:hidden>
            <s:hidden name="listaPreRequisitos" id="listaPreRequisitos" value="%{listaPreRequisitos}"></s:hidden>

            <s:textfield name="disciplina.codDisciplina" readonly="%{readOnlyCod}" label="Codigo da Disciplina" onkeyup="campoNumerico(this)" value="%{disciplina.codDisciplina}"></s:textfield>
            <s:textfield name="disciplina.nome" readonly="%{readOnly}" label="Nome da Disciplina" value="%{disciplina.nome}"></s:textfield>
            <s:select name="disciplina.periodo" readonly="%{readOnly}" label="Periodo" list="periodos" ></s:select>
            <s:select name="disciplina.curso.codCurso" readonly="%{readOnly}" label="Curso" listKey="codCurso" listValue="nome" 
                      list="cursos" ></s:select>
            <s:textfield name="disciplina.qtdeCreditos" readonly="%{readOnly}" label="Quantidade de Creditos" onkeyup="campoNumerico(this)" value="%{disciplina.qtdeCreditos}"></s:textfield>
            <s:textarea name="disciplina.ementa" readonly="%{readOnly}" cols="40" rows="10" label="Ementa" value="%{disciplina.ementa}"></s:textarea>

                <table border>
                    <tr>
                    <s:select name="preRequisitos" id="prerequisito" disabled="%{readOnly}" label="Pré-Requisitos" listKey="codDisciplina" listValue="nome" 
                              list="disciplinas" ></s:select>
                    </tr>

                    <tr>
                        <td colspan="2" align="center">
                            <input type="button" onclick="adicionaDisciplina()" id="adicionar" value="Adicionar"/>
                        </td>
                    </tr>

                    <tr>
                        <td colspan="2" align="center">
                            <table border="1" id="tbPreRequisitos">
                                <tr>
                                    <th>
                                        Cod. Disciplina
                                    </th>
                                    <th>
                                        Disciplina
                                    </th>

                                    <th>
                                        Ação
                                    </th>
                                </tr>

                            <s:iterator value="disciplina.disciplinasForCodDisciplinaPreRequisito">
                                <tr>
                                    <td>
                                        ${codDisciplina}
                                    </td>

                                    <td>
                                        ${nome}
                                    </td>

                                    <td>
                                        <a href="#" onclick="removeLinha(${codDisciplina})">Remover</a>
                                    </td>    
                                </tr>
                            </s:iterator>
                        </table>
            </table>
            <s:submit action="voltarDisciplina" value="Voltar"></s:submit>
            <s:submit action="confirmarDisciplina" value="Confimar"></s:submit>
        </s:form>

    </body>


</html>
